import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:oktoast/oktoast.dart';
import 'package:provider/provider.dart';
import 'package:wan_android_flutter/pages/auth/auth_vm.dart';

import '../../common_ui/common_style.dart';
import '../../routes/RouteUtils.dart';
import '../../routes/routes.dart';

class RegisterPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return RegisterPageState();
  }
}

class RegisterPageState extends State<RegisterPage> {
  AuthViewModel authViewModel = AuthViewModel();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
        backgroundColor: Colors.teal,
        body: ChangeNotifierProvider(
            create: (context) {
              return authViewModel;
            },
            child: Container(
              padding: EdgeInsets.only(left: 20.w, right: 20.w),
              alignment: Alignment.center,
              child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    commonInput("账号", onChanged: (value) {
                      authViewModel.registerInfo.name = value;
                    }),
                    SizedBox(
                      height: 20.h,
                    ),
                    commonInput("密码",obscureText: true, onChanged: (value) {
                      authViewModel.registerInfo.password = value;
                    }),
                    SizedBox(
                      height: 20.h,
                    ),
                    commonInput("确认密码",obscureText: true, onChanged: (value) {
                      authViewModel.registerInfo.rePassword = value;
                    }),
                    SizedBox(
                      height: 50.h,
                    ),
                    whiteBorderButton("点我注册", onTap: () {
                      authViewModel.register().then((onValue){
                        if(onValue == true){
                          showToast("注册成功");
                          RouteUtils.pop(context);
                        }
                      });
                    }),
                    _registerButton("返回登录", () {
                      RouteUtils.pushForNamed(context, RoutesPath.loginPage);
                    }),
                  ]),
            )));
  }

  Widget _registerButton(String? title, GestureTapCallback? onTap) {
    return GestureDetector(
        onTap: onTap,
        child: Container(
          width: 100.w,
          height: 45.h,
          alignment: Alignment.center,
          child: Text(
            title ?? "",
            style: TextStyle(color: Colors.white, fontSize: 13.sp),
          ),
        ));
  }
}
